﻿namespace Magic.System;

[Injection]
public class CodeGeneratorService : ICodeGeneratorService
{
    public DatabaseListOutput GetDatabaseList()
    {
        DatabaseListOutput result = new DatabaseListOutput();
        var list = SugarContext.Instance.DbMaintenance.GetTableInfoList(false);
        list.ForEach(item =>
        {
            result.Dbs.Add(item.Name, $"{item.Name} - {item.Description}");
        });

        return result;
    }

    public ParseFieldDataOutput ParseFieldData(ColumnInfosByTableNameInput input)
    {
        if (string.IsNullOrWhiteSpace(input.Table))
            return new ParseFieldDataOutput();
        var list = SugarContext.Instance.DbMaintenance.GetColumnInfosByTableName(input.Table, false);

        var result = new ParseFieldDataOutput() { Columns=new Dictionary<string, DbColumnInfoItem>(),Comment=input.Table};
        list.ForEach(item =>
        {
            result.Columns.Add(item.DbColumnName,new DbColumnInfoItem
            {
                AutoIncrement = item.IsIdentity,
                Comment = item.ColumnDescription,
                DataType = item.DataType,
                DesignType = "string",
                Name = item.DbColumnName,
                Type = item.DataType,
                Default = item.DefaultValue,
                Null = item.IsNullable,
                PrimaryKey = item.IsPrimarykey,
                Unsigned = item.IsUnsigned
            });
        });

        return result;
    }
}
